package com.wdh.kjs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.Adapter.QuestionListViewAdapter;
import com.wdh.kjs.Adapter.QuestionListViewAdapter2;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.Question;
import com.wdh.kjs.model.QuestionAnswerForAppModel;
import com.wdh.kjs.model.QuestionListForApp;
import com.wdh.kjs.util.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class KehuAnswerActivity extends BaseTopActivity{
    private String partyId;
    private String businessId;
    private String questionDescribe;

    private ListView lv_question;
    private ArrayList<Question>answerList=null;
    private Map<String,String> describeToAnswer=new HashMap<String,String>();
    private QuestionAnswerForAppModel questionAnswerForAppModel;
    private QuestionListViewAdapter2 mAdapter;
    private int count=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kehu_answer);
        getEtrExtra(getIntent());
        initView();
        count=1;
        initAnswerList();
    }

    private void initView(){
        initTopBar("服务中心");
        lv_question=(ListView) findViewById(R.id.kehu_answer);
        lv_question.setOnItemClickListener(new MyListViewItemlistener());
    }

    private void getEtrExtra(Intent intent){
        partyId=intent.getStringExtra("partyId");
        businessId=intent.getStringExtra("businessId");
        questionDescribe=intent.getStringExtra("questionDescribe");
    }

    private void initAnswerList() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("businessId", businessId + "");
        params.addBodyParameter("questionDescribe", questionDescribe + "");
        params.addBodyParameter("partyId", partyId + "");

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_GetQuestionAnswer_For_App, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                questionAnswerForAppModel = gson.fromJson(responseInfo.result, QuestionAnswerForAppModel.class);
               if(questionAnswerForAppModel.getSuccess()==true){
                   answerList=questionAnswerForAppModel.getAnswer();
                   addDescribeToAnswer(answerList);
                   mAdapter=new QuestionListViewAdapter2(KehuAnswerActivity.this,answerList);
                   lv_question.setAdapter(mAdapter);
               }
                else {
                    ToastUtils.showShort(KehuAnswerActivity.this, questionAnswerForAppModel.getMsg());
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(KehuAnswerActivity.this, "网络异常！");
            }
        });
    }

    private void addDescribeToAnswer(ArrayList<Question> arrayList){
        for(Question q:arrayList){
            q.setQuestionDescribe(count+"."+q.getQuestionDescribe());
            count++;
            describeToAnswer.put(q.getQuestionDescribe(),q.getQuestionResponse());
        }
    }

    class MyListViewItemlistener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String questionDescribe=answerList.get(position).getQuestionDescribe();
            String answer=describeToAnswer.get(questionDescribe);
            //去掉“数字.”
            String str[]=questionDescribe.split("\\.");
            questionDescribe="";
            int i;
            for(i=1;i<str.length;i++){
                questionDescribe=questionDescribe+str[i];
            }
            QuestionListForApp.staticTitle=questionDescribe;
            QuestionListForApp.staticAnswer=answer;
            Intent intent=new Intent(KehuAnswerActivity.this, KefuAnswerQuestionActivity.class);
            startActivity(intent);
        }
    }
}
